<?php


function GetTimeRangeStart($day, $timeUnit)
{
  $day_timestamp = strtotime($day . ' 00:00:00');
  
  switch($timeUnit)
  {
    case 'week':
      $day_of_week = date('N', $day_timestamp) - 1;
      $range_start_timestamp = strtotime("-{$day_of_week} day", strtotime(date("Y-m-d 00:00:00", $day_timestamp)));
      return date("Y-m-d", $range_start_timestamp);
    
    case 'month':
      $range_start_timestamp = strtotime(date("Y-m-01 00:00:00", $day_timestamp));
      return date("Y-m-d", $range_start_timestamp);
    
    case 'day':
    return $day;
  }
}

function GetTimeRangeEnd($day, $timeUnit)
{
  $day_timestamp = strtotime($day . ' 00:00:00');
  
  switch($timeUnit)
  {
    case 'week':
      $week_start_timestamp = strtotime(GetTimeRangeStart($day, $timeUnit));
      $range_end_timestamp = strtotime("+6 day", strtotime(date("Y-m-d 23:59:59", $week_start_timestamp)));
      return date("Y-m-d", $range_end_timestamp);
    
    case 'month':
      $currentMonth = date('n', $day_timestamp);
      $currentYear = date('Y', $day_timestamp);
      $currentHour = date('H', $day_timestamp);
      $currentMin = date('i', $day_timestamp);
      $currentSec = date('s', $day_timestamp);
      $range_end_timestamp = mktime($currentHour, $currentMin, $currentSec - 1, $currentMonth + 1, 1, $currentYear);
      return date("Y-m-d", $range_end_timestamp);
    
    case 'day':
      $currentMonth = date('n', $day_timestamp);
      $currentYear = date('Y', $day_timestamp);
      $currentDay = date('j', $day_timestamp);
      $range_end_timestamp = mktime(23, 59, 59, $currentMonth, $currentDay, $currentYear);
      return date("Y-m-d", $range_end_timestamp);
  }
}


function GetNextTimeRangeStart($day, $timeUnit)
{
  $day_timestamp = strtotime($day . ' 00:00:00');
  
  switch($timeUnit)
  {
    case 'week':
      $prev_week_start_timestamp = strtotime(GetTimeRangeStart($day, $timeUnit));
      $next_range_start_timestamp = strtotime("+7 day", strtotime(date("Y-m-d 00:00:00", $prev_week_start_timestamp)));
      return date("Y-m-d", $next_range_start_timestamp);
    
    case 'month':
      $currentMonth = date('n', $day_timestamp);
      $currentYear = date('Y', $day_timestamp);
      $next_range_start_timestamp = mktime(00, 00, 00, $currentMonth + 1, 1, $currentYear);
      return date("Y-m-d", $next_range_start_timestamp);
    
    case 'day':
      $currentMonth = date('n', $day_timestamp);
      $currentYear = date('Y', $day_timestamp);
      $currentDay = date('j', $day_timestamp);
      $next_range_start_timestamp = mktime(00, 00, 00, $currentMonth, $currentDay + 1, $currentYear);
      return date("Y-m-d", $next_range_start_timestamp);
  }
}


function Maxim($value, $datefrom, $dateto, $this, $timeUnit)
{
  $max = $this['value'];
  if($timeUnit == 'day')
    {
      if($value > $max)
        {
          $max = $value;
          return array('value'=>$max, 'date' =>$datefrom);
        }
        else return $this;
    }
    
   if($timeUnit != 'day')
     {
      if($value > $max)
        {
          $max = $value;
          $range = $datefrom." - ".$dateto;
          return array('value'=>$max, 'date' =>$range);
        }
      else return $this;
     }  
}

function Minim($value, $datefrom, $dateto, $this, $timeUnit)
{
  $min = $this['value'];
  
  if($timeUnit == 'day')
    {
      if($value < $min)
        {
          $min = $value;
          return array('value'=>$min, 'date' =>$datefrom);
        }
        else return $this;
    }
    
   if($timeUnit != 'day')
     {
      if($value < $min)
        {
          $min = $value;
          $range = $datefrom." - ".$dateto;
          return array('value'=>$min, 'date' =>$range);
        }
      else return $this;
     }  
}

function GetCategories($dateFrom, $dateTo)
{
  $categories = array();
  $columns = "categories.name AS `category_name`, categories.id as `category_id`, SUM(item_daily_views.views) AS nr_views";
  $from = "item_daily_views INNER JOIN items ON item_daily_views.item_id = items.id
  INNER JOIN categories ON items.category_id = categories.id";
  
  $where = "item_daily_views.day_stamp>=(TO_DAYS('$dateFrom')-719528) AND 
  item_daily_views.day_stamp<=(TO_DAYS('$dateTo')-719528)  AND categories.status = 1";
  
  $query = "SELECT $columns FROM $from WHERE $where GROUP BY `category_name` ORDER BY `nr_views` DESC";
  $categories = ExecuteQuery($query); 
  return $categories;
}

?>